package com.irdstudio.efp.esb.api.service.psd.apply;

import com.alibaba.fastjson.JSON;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.constant.EsbRetCodeStandard;
import com.irdstudio.basic.framework.core.constant.SDicCertTypeEnum;
import com.irdstudio.basic.framework.core.exception.BizException;

import com.irdstudio.basic.framework.core.exception.OcmSystemException;
import com.irdstudio.basic.framework.core.util.Amount2RMB;
import com.irdstudio.efp.console.common.psd.PsdConstantSet;
import com.irdstudio.efp.console.service.facade.PrdInfoLprService;
import com.irdstudio.efp.console.service.facade.SOrgService;
import com.irdstudio.efp.console.service.vo.PrdInfoLprVO;
import com.irdstudio.efp.console.service.vo.SOrgVO;
import com.irdstudio.efp.cus.service.facade.CusIndivService;
import com.irdstudio.efp.cus.service.vo.CusIndivVO;
import com.irdstudio.efp.esb.api.bean.psd.query.resp.ContMsgAssembleRespBean;
import com.irdstudio.efp.esb.service.vo.DkContMsgAssembleVO;
import com.irdstudio.efp.limit.service.facade.LmtPrdContService;
import com.irdstudio.efp.limit.service.vo.LmtPrdContVO;
import com.irdstudio.efp.nls.service.common.rules.apply.ApplyExistRule;
import com.irdstudio.efp.nls.service.common.rules.credit.HasCreditInfoRule;
import com.irdstudio.efp.nls.service.facade.NlsApplyInfoService;
import com.irdstudio.efp.nls.service.facade.sx.NlsCreditInfoService;
import com.irdstudio.efp.nls.service.vo.NlsApplyInfoVO;
import com.irdstudio.efp.nls.service.vo.sx.NlsCreditInfoVO;
import com.sleepycat.je.tree.IN;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Objects;


/**
 * 需求编号: 普税贷借款合同要素封装服务
 * 问题编号:
 * 开发人员: xieziwei
 * 创建日期: 2020/6/10
 * 功能描述:
 * 修改日期: 2020/6/10
 * 修改描述:
 */
@Service("dkContMsgAssembleService")
public class DkContMsgAssembleService implements FrameworkService {
    private static Logger logger = LoggerFactory.getLogger(DkContMsgAssembleService.class);

    public DkContMsgAssembleVO assembleTask(String applySeq,String lmtApplySeq, double loanAmt,String repayMethod) throws BizException {
        DkContMsgAssembleVO dkContMsgAssembleVO = new DkContMsgAssembleVO();

        // 授信表信息
        NlsCreditInfoVO nlsCreditInfoVO = nlsCreditInfoService.queryByLmtApplySeq(lmtApplySeq);
        if (Objects.isNull(nlsCreditInfoVO)){
            throw new BizException(EsbRetCodeStandard.WFJKYWGZ.getValue(), "授信信息不存在，请核查！");
        }
        //  客户表信息
        CusIndivVO inCusIndivVO = new CusIndivVO();
        inCusIndivVO.setCusId(nlsCreditInfoVO.getCusId());
        CusIndivVO cusIndivVO = cusIndivService.queryByPk(inCusIndivVO);


        if (Objects.isNull(cusIndivVO)){
            throw new BizException(EsbRetCodeStandard.KHXXJYBTG.getValue(), "客户信息不存在，请核查！");
        }

        dkContMsgAssembleVO.setCusName(nlsCreditInfoVO.getCusName());
        dkContMsgAssembleVO.setCertType(SDicCertTypeEnum.getDesc(nlsCreditInfoVO.getCertType()));
        dkContMsgAssembleVO.setCertCode(nlsCreditInfoVO.getCertCode());

        dkContMsgAssembleVO.setCusAddr(cusIndivVO.getFamilyAddr());
        dkContMsgAssembleVO.setCusZipcode(cusIndivVO.getPostCode());
        dkContMsgAssembleVO.setCusPhone(cusIndivVO.getPhone());
        dkContMsgAssembleVO.setCusEmail(cusIndivVO.getEmail());
        dkContMsgAssembleVO.setCusTax(cusIndivVO.getFaxCode());

        dkContMsgAssembleVO.setContNo(applySeq);
        dkContMsgAssembleVO.setZhAddr(PsdConstantSet.ZH_ADDR);
        dkContMsgAssembleVO.setZhZipcode(PsdConstantSet.ZH_POST);
        dkContMsgAssembleVO.setZhTel(PsdConstantSet.ZH_TEL);

        String amount = new BigDecimal(loanAmt).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
        dkContMsgAssembleVO.setLmtAmtUpper(Amount2RMB.convert(amount));
        dkContMsgAssembleVO.setLmtAmtLower(amount);
        dkContMsgAssembleVO.setOrgName(getSOrg(nlsCreditInfoVO.getNlsOperOrgid()).getOrgName());
        dkContMsgAssembleVO.setRecvNo(nlsCreditInfoVO.getRecvAccount());

        dkContMsgAssembleVO.setLmtContNo(lmtApplySeq);
        // 还款方式
        dkContMsgAssembleVO.setRepayMethod("101".equals(repayMethod)?"1":"2");
        // 年利率
        dkContMsgAssembleVO.setyRate( Objects.nonNull(nlsCreditInfoVO.getApproveRateY()) ? nlsCreditInfoVO.getApproveRateY().multiply(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_HALF_UP).toString() : "0.00");
        // 查询当月的lpr利率
        PrdInfoLprVO prdInfoLprVO = prdInfoLprService.queryByIntratecdAndTerm(new PrdInfoLprVO());
        if (Objects.isNull(prdInfoLprVO)){
            throw new BizException("查询lpr利率为空，请核查！");
        }
        // (固定利率 - 当月LPR利率）* 10000
        if (prdInfoLprVO != null) {
            // 贷款市场报价利率（LPR）quoteRates
            dkContMsgAssembleVO.setQuoteRates(prdInfoLprVO.getIntrate().multiply(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            BigDecimal loanRate = nlsCreditInfoVO.getApplyRateY().multiply(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_HALF_UP);
            // operator
            String operator = loanRate.subtract(prdInfoLprVO.getIntrate()).multiply(new BigDecimal("100")).signum()==1?"加":"减";
            dkContMsgAssembleVO.setOperator(operator);
            // LRPR
            String lprRate = loanRate.subtract(prdInfoLprVO.getIntrate()).multiply(new BigDecimal("100")).abs().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
            lprRate = loanRate.subtract(prdInfoLprVO.getIntrate()).multiply(new BigDecimal("100")).signum()==1?"+"+lprRate:"-"+lprRate;
            dkContMsgAssembleVO.setLRPR(lprRate);


        }
        //签约日期 yyyy-MM-dd(当前日期)
        dkContMsgAssembleVO.setSignDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));

        return dkContMsgAssembleVO;
    }

    public ContMsgAssembleRespBean getSxCtrInfo(String lmtApplySeq,ContMsgAssembleRespBean resp) throws BizException {
        NlsCreditInfoVO nlsCreditInfoVO = nlsCreditInfoService.queryByLmtApplySeq(lmtApplySeq);
        if (Objects.isNull(nlsCreditInfoVO)) {
            throw new BizException("根据流水号查询到的授信信息为空");
        }
        CusIndivVO pkParam = new CusIndivVO();
        pkParam.setCusId(nlsCreditInfoVO.getCusId());
        CusIndivVO cusIndivVO = cusIndivService.queryByPk(pkParam);
        if (Objects.isNull(nlsCreditInfoVO)) {
            throw new BizException("客户信息为空");
        }
        resp.setContNo(lmtApplySeq);
        resp.setaAddr(PsdConstantSet.ZH_ADDR);
        resp.setaPsno(PsdConstantSet.ZH_POST);
        resp.setaTelNo(PsdConstantSet.ZH_TEL);
        resp.setaCertType(getCertTyName(cusIndivVO.getCertType()));
        resp.setbAddr(cusIndivVO.getIndivRsdAddr());
        resp.setbPsno(cusIndivVO.getPostCode());
        resp.setbTelNo(cusIndivVO.getPhone());
        resp.setbEmail(cusIndivVO.getEmail());
        resp.setbFax(cusIndivVO.getFaxCode());
        resp.setbCertNo(cusIndivVO.getCertCode());
        resp.setbCusName(cusIndivVO.getCusName());
        resp.setCertNo(cusIndivVO.getCertCode());
        resp.setContractDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));
        return resp;
    }



    private SOrgVO getSOrg(String orgCode) throws BizException {
        SOrgVO sOrgVO = new SOrgVO();
        sOrgVO.setOrgCode(orgCode);
        sOrgVO = sOrgService.queryByPk(sOrgVO);
        if (Objects.isNull(sOrgVO)) {
            throw new BizException("通过机构号【" + orgCode + "】，获取到机构信息为空！");
        }
        return sOrgVO;
    }

    //根据证件类型获取证件中文名
    public static String getCertTyName(String certTy) {
        if ("00000".equals(certTy)) {
            return "未知证件类型";
        }

        if ("10000".equals(certTy)) {
            return "对私身份证件";
        }

        if ("10100".equals(certTy)) {
            return "居民身份证";
        }

        if ("10200".equals(certTy)) {
            return "临时身份证";
        }

        if ("10300".equals(certTy)) {
            return "户口簿";
        }

        if ("10400".equals(certTy)) {
            return "护照";
        }

        if ("10401".equals(certTy)) {
            return "普通护照";
        }

        if ("10402".equals(certTy)) {
            return "外交护照";
        }

        if ("10403".equals(certTy)) {
            return "公务护照";
        }

        if ("10404".equals(certTy)) {
            return "因公普通护照";
        }

        if ("10405".equals(certTy)) {
            return "香港特别行政区护照";
        }

        if ("10406".equals(certTy)) {
            return "澳门特别行政区护照";
        }

        if ("10407".equals(certTy)) {
            return "台湾护照";
        }

        if ("10408".equals(certTy)) {
            return "外国护照";
        }

        if ("10500".equals(certTy)) {
            return "军人身份证件";
        }

        if ("10501".equals(certTy)) {
            return "士兵证";
        }

        if ("10502".equals(certTy)) {
            return "军官证";
        }

        if ("10503".equals(certTy)) {
            return "文职干部证";
        }

        if ("10504".equals(certTy)) {
            return "军官退休证";
        }

        if ("10505".equals(certTy)) {
            return "文职干部退休证";
        }

        if ("10506".equals(certTy)) {
            return "军事学员证";
        }

        if ("10507".equals(certTy)) {
            return "军队离休干部荣誉证";
        }

        if ("10600".equals(certTy)) {
            return "武警身份证件";
        }

        if ("10601".equals(certTy)) {
            return "武警士兵证";
        }

        if ("10602".equals(certTy)) {
            return "警官证";
        }

        if ("10603".equals(certTy)) {
            return "武警文职干部证";
        }

        if ("10604".equals(certTy)) {
            return "武警军官退休证";
        }

        if ("10605".equals(certTy)) {
            return "武警文职干部退休证";
        }

        if ("10700".equals(certTy)) {
            return "港澳台居民有效身份证件";
        }

        if ("10701".equals(certTy)) {
            return "港澳居民来往内地通行证";
        }

        if ("10702".equals(certTy)) {
            return "港澳居民身份证";
        }

        if ("10703".equals(certTy)) {
            return "台湾居民来往大陆通行证";
        }

        if ("10704".equals(certTy)) {
            return "台湾居民来往大陆通行证（一次有效）";
        }

        if ("10705".equals(certTy)) {
            return "台湾居民身份证";
        }

        if ("10706".equals(certTy)) {
            return "台湾居民定居证";
        }

        if ("10707".equals(certTy)) {
            return "澳门特别行政区旅行证";
        }

        if ("10708".equals(certTy)) {
            return "香港居民身份证";
        }

        if ("10709".equals(certTy)) {
            return "澳门居民身份证";
        }

        if ("10800".equals(certTy)) {
            return "内地居民往来港澳台有效身份证件";
        }

        if ("10801".equals(certTy)) {
            return "往来港澳通行证";
        }

        if ("10802".equals(certTy)) {
            return "前往港澳通行证";
        }

        if ("10803".equals(certTy)) {
            return "大陆居民往来台湾通行证";
        }

        if ("10804".equals(certTy)) {
            return "因公往来香港澳门特别行政区通行证";
        }

        if ("10900".equals(certTy)) {
            return "外国人永久居留证";
        }

        if ("11000".equals(certTy)) {
            return "外国人居留证";
        }

        if ("11100".equals(certTy)) {
            return "驾驶证";
        }

        if ("11101".equals(certTy)) {
            return "机动车驾驶证";
        }

        if ("11102".equals(certTy)) {
            return "飞机驾驶证";
        }

        if ("11103".equals(certTy)) {
            return "船舶驾驶证";
        }

        if ("11200".equals(certTy)) {
            return "边民出入境通行证";
        }

        if ("12300".equals(certTy)) {
            return "学生证";
        }

        if ("12400".equals(certTy)) {
            return "退休证";
        }

        if ("12500".equals(certTy)) {
            return "工作证";
        }

        if ("12600".equals(certTy)) {
            return "执行公务证";
        }

        if ("12700".equals(certTy)) {
            return "回乡证";
        }

        if ("12800".equals(certTy)) {
            return "文职证";
        }

        if ("19999".equals(certTy)) {
            return "其它个人证件";
        }

        if ("20000".equals(certTy)) {
            return "对公证件";
        }

        if ("20100".equals(certTy)) {
            return "统一社会信用代码";
        }

        if ("20200".equals(certTy)) {
            return "企业登记代码";
        }

        if ("20300".equals(certTy)) {
            return "工商登记证";
        }

        if ("20400".equals(certTy)) {
            return "股东代码证";
        }

        if ("20500".equals(certTy)) {
            return "企业营业执照";
        }

        if ("20600".equals(certTy)) {
            return "组织机构代码证";
        }

        if ("20700".equals(certTy)) {
            return "税务登记证";
        }

        if ("20701".equals(certTy)) {
            return "税务登记证（国税）";
        }

        if ("20702".equals(certTy)) {
            return "税务登记证（地税）";
        }

        if ("20800".equals(certTy)) {
            return "域名注册证";
        }

        if ("20900".equals(certTy)) {
            return "开户许可证";
        }

        if ("20901".equals(certTy)) {
            return "军队开户许可证";
        }

        if ("21000".equals(certTy)) {
            return "外汇许可证";
        }

        if ("21100".equals(certTy)) {
            return "企业外管代码";
        }

        if ("21200".equals(certTy)) {
            return "中征码";
        }

        if ("21300".equals(certTy)) {
            return "金融许可证号";
        }

        if ("21400".equals(certTy)) {
            return "保险业务许可证";
        }

        if ("21500".equals(certTy)) {
            return "证券业务许可证";
        }

        if ("21600".equals(certTy)) {
            return "证监会代码证号";
        }

        if ("21700".equals(certTy)) {
            return "进出口经营权许可证书";
        }

        if ("21800".equals(certTy)) {
            return "外商企业投资登记证书";
        }

        if ("21900".equals(certTy)) {
            return "审批文号";
        }

        if ("22000".equals(certTy)) {
            return "技术监督局编号";
        }

        if ("22100".equals(certTy)) {
            return "人行金融机构代码";
        }

        if ("22200".equals(certTy)) {
            return "银行swift bic";
        }

        if ("22300".equals(certTy)) {
            return "联行行号";
        }

        if ("22400".equals(certTy)) {
            return "外汇管理局印单局代码";
        }

        if ("22500".equals(certTy)) {
            return "FCI编号";
        }

        if ("22600".equals(certTy)) {
            return "事业法人证书";
        }

        if ("22700".equals(certTy)) {
            return "收费许可证";
        }

        if ("22800".equals(certTy)) {
            return "资质证书";
        }

        if ("22900".equals(certTy)) {
            return "下属机构证书";
        }

        if ("23100".equals(certTy)) {
            return "支付业务许可证";
        }

        if ("23200".equals(certTy)) {
            return "社团法人证书";
        }

        if ("23300".equals(certTy)) {
            return "民办非企业登记证";
        }

        if ("23400".equals(certTy)) {
            return "外国驻华机构登记证";
        }

        if ("23500".equals(certTy)) {
            return "外地常设机构登记证";
        }

        if ("23600".equals(certTy)) {
            return "特殊机构代码";
        }

        if ("23700".equals(certTy)) {
            return "行政机关";
        }

        if ("23800".equals(certTy)) {
            return "社会团体";
        }

        if ("23900".equals(certTy)) {
            return "登记文件";
        }

        if ("24100".equals(certTy)) {
            return "批文或批复";
        }

        if ("24200".equals(certTy)) {
            return "开户证明";
        }

        if ("24300".equals(certTy)) {
            return "企业名称预先核准通知书";
        }

        if ("24400".equals(certTy)) {
            return "事业社团法人证书";
        }

        if ("24500".equals(certTy)) {
            return "高新技术企业认定证书";
        }

        if ("24600".equals(certTy)) {
            return "建筑业企业资质证书";
        }

        if ("24700".equals(certTy)) {
            return "社会福利企业证书";
        }

        if ("24800".equals(certTy)) {
            return "机构信用代码";
        }

        if ("29999".equals(certTy)) {
            return "其它对公证件";
        }

        if ("99999".equals(certTy)) {
            return "其它证件";
        }

        if ("10409".equals(certTy)) {
            return "外交官证";
        }

        if ("10410".equals(certTy)) {
            return "使（领事）馆证";
        }

        if ("11104".equals(certTy)) {
            return "海员证";
        }

        if ("10411".equals(certTy)) {
            return "外国人身份证件";
        }

        if ("12301".equals(certTy)) {
            return "高校毕业生自主创业证";
        }

        if ("12302".equals(certTy)) {
            return "就业失业登记证";
        }

        if ("12304".equals(certTy)) {
            return "离休证";
        }

        if ("10507".equals(certTy)) {
            return "城镇退役士兵自谋职业证";
        }

        if ("10508".equals(certTy)) {
            return "随军家属身份证明";
        }

        if ("10509".equals(certTy)) {
            return "中国人民解放军军官转业证书";
        }

        if ("10510".equals(certTy)) {
            return "中国人民解放军义务兵退出现役证";
        }

        if ("10511".equals(certTy)) {
            return "中国人民解放军士官退出现役证";
        }

        if ("12303".equals(certTy)) {
            return "就业创业证";
        }

        if ("10301".equals(certTy)) {
            return "医学出生证明";
        }

        return "身份证";

    }


    @Autowired
    @Qualifier("nlsCreditInfoService")
    private NlsCreditInfoService nlsCreditInfoService;

    @Autowired
    @Qualifier("nlsApplyInfoService")
    private NlsApplyInfoService nlsApplyInfoService;

    @Autowired
    @Qualifier("cusIndivService")
    private CusIndivService cusIndivService;

    @Autowired
    @Qualifier("lmtPrdContService")
    private LmtPrdContService lmtPrdContService;

    @Autowired
    @Qualifier("prdInfoLprService")
    private PrdInfoLprService prdInfoLprService;

    @Autowired
    @Qualifier("sOrgService")
    private SOrgService sOrgService;

}
